<template>
  <!-- 计划修工单表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="1000px"
    draggable
    @close="closeDialog"
    style="max-height: 950px"
  >
    <div style="max-height: 60vh; overflow-y: auto">
      <h3 class="mb-[5px] mt-[-5px] c-[#333] text-[15px] font-600 lh-[28px]">
        基本数据
      </h3>
      <el-descriptions class="margin-top" :column="3" size="default" border>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">工单编号</div>
          </template>
          {{ formData.orderNo }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">维修状态</div>
          </template>
          {{ useDictionary("RepairOrderStatus", formData.orderStatus) }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">计划开始时间</div>
          </template>
          {{
            formData.planStartTime
              ? dayjs(formData.planStartTime).format("YYYY-MM-DD")
              : ""
          }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">模具编号</div>
          </template>
          {{ formData.assetCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">模具名称</div>
          </template>
          {{ formData.assetName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">位置</div>
          </template>
          {{ formData.locationName }}
        </el-descriptions-item>
        <!-- <el-descriptions-item>
          <template #label>
            <div class="cell-item">维修执行班组</div>
          </template>
          {{ formData.executeTeamName }}
        </el-descriptions-item> -->
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">维修参与人</div>
          </template>
          <!-- {{
            formData.repairOrderPersons
              ?.map((item) => item.personName)
              .join(",")
          }} -->
          {{ formData.maintainUser }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">主修人</div>
          </template>
          {{ formData.repairUserName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">维修开始时间</div>
          </template>
          {{ formData.repairStartTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">维修结束时间</div>
          </template>
          {{ formData.repairEndTime }}
        </el-descriptions-item>
        <el-descriptions-item v-if="formData.repairUseTime">
          <template #label>
            <div class="cell-item">维修用时</div>
          </template>
          {{ formData.repairUseTime || "" }}
        </el-descriptions-item>

        <!--      <el-descriptions-item>-->
        <!--        <template #label>-->
        <!--          <div class="cell-item">维修类型</div>-->
        <!--        </template>-->
        <!--        {{ useDictionary("PlanRepairType", formData.repairType) }}-->
        <!--      </el-descriptions-item>-->
        <el-descriptions-item :span="3">
          <template #label>
            <div class="cell-item">维修内容</div>
          </template>
          {{ formData.repairContent }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template #label>
            <div class="cell-item">维修情况说明</div>
          </template>
          {{ formData.repairSituation }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template #label>
            <div class="cell-item">其他问题</div>
          </template>
          {{ formData.otherProblem }}
        </el-descriptions-item>
      </el-descriptions>
      <div v-if="formData.parts && formData.parts.length">
        <h3 class="mb-[5px] c-[#333] text-[15px] font-600 lh-[28px]">
          更换备件
        </h3>
        <el-table
          :data="formData.parts"
          highlight-current-row
          border
          :cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="备件编号" prop="spareCode" width="175" />
          <el-table-column label="备件名称" prop="spareName" />
          <el-table-column label="备件类型" prop="spareType" width="175">
            <template #default="scope">
              {{
                dictionaryStore.filterDictName("SpareType", scope.row.spareType)
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="使用数量"
            prop="sparePartNumber"
            width="175"
          />
          <el-table-column label="数量单位" prop="spareUnit" width="175" />
        </el-table>
      </div>

      <div v-if="formData.fixBeforeFiles && formData.fixBeforeFiles.length">
        <h3 class="mb-[5px] c-[#333] text-[15px] font-600 lh-[28px]">
          维修前图片
        </h3>
        <div :key="formData.id">
          <CommonUploadFile
            :showText="false"
            :isRead="true"
            :files="formData.fixBeforeFiles"
          />
        </div>
      </div>
      <div v-if="formData.fixAfterFiles && formData.fixAfterFiles.length">
        <h3 class="mb-[5px] mt-[-5px] c-[#333] text-[15px] font-600 lh-[28px]">
          维修后图片
        </h3>
        <div :key="formData.id">
          <CommonUploadFile
            :showText="false"
            :isRead="true"
            :files="formData.fixAfterFiles"
          />
        </div>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          v-if="formData.orderStatus == 2"
          >通过</el-button
        >
        <el-button
          type="primary"
          :loading="loading"
          @click="noHandleSubmit"
          v-if="formData.orderStatus == 2"
          >不通过</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import dayjs from "dayjs";
import { useDictionary } from "@/utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMEAMPLANREPAIRORDERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {}, 50);
  }
);

const activeNames = ref(["1", "2"]);

let formData = reactive({
  id: "",
  version: "",
  planId: "",
  planName: "",
  assetCode: "",
  assetName: "",
  locationName: "",
  assetTypeCode: "",
  assetTypeName: "",
  planDetailId: "",
  orderNo: "",
  planStartTime: "",
  repairStartTime: "",
  repairEndTime: "",
  repairUseTime: "",
  repairType: "",
  repairContent: "",
  repairSituation: "",
  otherProblem: "",
  executeTeamId: "",
  executeTeamName: "",
  repairOrderPersons: "",
  repairUserName: "",
  orderStatus: "",
  fixBeforeFiles: [],
  fixAfterFiles: [],
  maintainUser: "",
  parts: [],
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        planId,
        planName,
        assetCode,
        assetName,
        locationName,
        assetTypeCode,
        assetTypeName,
        planDetailId,
        orderNo,
        planStartTime,
        repairStartTime,
        repairEndTime,
        repairUseTime,
        repairType,
        repairContent,
        repairSituation,
        otherProblem,
        executeTeamId,
        executeTeamName,
        repairOrderPersons,
        repairUserName,
        orderStatus,
        fixBeforeFiles,
        fixAfterFiles,
        maintainUser,
        parts,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        planId,
        planName,
        assetCode,
        assetName,
        locationName,
        assetTypeCode,
        assetTypeName,
        planDetailId,
        orderNo,
        planStartTime,
        repairStartTime,
        repairEndTime,
        repairUseTime,
        repairType,
        repairContent,
        repairSituation,
        otherProblem,
        executeTeamId,
        executeTeamName,
        repairOrderPersons,
        repairUserName,
        orderStatus,
        fixBeforeFiles,
        fixAfterFiles,
        maintainUser,
        parts,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formData.id = "";
  formData.version = "";
  formData.planId = "";
  formData.planName = "";
  formData.assetCode = "";
  formData.assetName = "";
  formData.assetTypeCode = "";
  formData.assetTypeName = "";
  formData.planDetailId = "";
  formData.orderNo = "";
  formData.planStartTime = "";
  formData.repairStartTime = "";
  formData.repairEndTime = "";
  formData.repairUseTime = "";
  formData.repairType = "";
  formData.repairContent = "";
  formData.repairSituation = "";
  formData.otherProblem = "";
  formData.executeTeamId = "";
  formData.executeTeamName = "";
  formData.repairUserName = "";
  formData.orderStatus = "";
  formData.fixBeforeFiles = [];
  formData.fixAfterFiles = [];
  formData.maintainUser = "";
  formData.parts = [];
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);
async function handleSubmit() {
  loading.value = true;
  await request({
    url: "/eam/equplanrepairorder/passById",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  emit("reset-table", formData.id || "");
  closeDialog();
}
async function noHandleSubmit() {
  loading.value = true;
  await request({
    url: "/eam/equplanrepairorder/noPassById",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  emit("reset-table", formData.id || "");
  closeDialog();
}
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
